package com.sca.order.filter;

import com.sca.order.utils.EnvUtil;
import com.sca.order.utils.JwtUtil;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 单机调试环境时开启token解析，可绕过网关直接请求
 *
 * @author liuxianfei
 * @since 2020/11/10 10:33
 */
public class ResolveTokenToHeaderFilter extends OncePerRequestFilter {

    private final EnvUtil envUtil;

    public ResolveTokenToHeaderFilter(EnvUtil envUtil) {
        this.envUtil = envUtil;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        if (envUtil.isDev()) {
            request = JwtUtil.appendRequestHeader(request);
        }
        chain.doFilter(request, response);
    }

}
